set nocompatible
filetype on
filetype indent on
filetype plugin on
filetype plugin indent on
set encoding=utf-8
let &t_ut=''
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set laststatus=2
set autochdir
set number
set relativenumber
set clipboard+=unnamedplus
set scrolloff=10
syntax on
set cursorline
set wrap
set textwidth=60
set showcmd
set wildmenu
let mapleader = ","
set hlsearch
set smartcase
set splitbelow
set splitright

inoremap <leader>w <esc>:w<cr>
inoremap <leader>f <esc>la
inoremap <leader>g <esc>lla
inoremap <leader>a <esc>$a
inoremap jf <Esc>

nnoremap <leader>w <esc>:w<cr>
nnoremap <leader>q <esc>:q<cr>
nnoremap <leader>Q <esc>:qa<cr>
nnoremap <leader>g :nohl<cr>
nnoremap <leader>f /
nnoremap <leader>a <esc>$a
nnoremap zl <cr>:vsplit<cr>
nnoremap zj <cr>:split<cr>
nnoremap J <c-d>zz
nnoremap K <c-u>zz
nnoremap <c-l> <c-w>l
nnoremap <c-h> <c-w>h
nnoremap <c-d> <nop>
nnoremap <c-u> <nop>
nnoremap r <nop>
nnoremap + :res +5<cr>
nnoremap - :res -5<cr>
nnoremap - :vertical resize-5<cr>
nnoremap = :vertical resize+5<cr>
nnoremap gh 0
nnoremap gl $
nnoremap gJ G
nnoremap gK gg
nnoremap gg <nop>
nnoremap gj 6j
nnoremap gk 6k
nnoremap s f
nnoremap tn :tabe<cr>
nnoremap tl :+tabnext<cr>
nnoremap th :-tabnext<cr>
nnoremap n nzz
nnoremap N Nzz
nnoremap d "cd
nnoremap dd "cdd
nnoremap x "cx
nnoremap c "cc
nnoremap dw "cdw
nnoremap cw "ccw
nnoremap P "cp

vnoremap gh 0
vnoremap gl $
vnoremap gj 6j
vnoremap gk 6k
vnoremap q <esc>
vnoremap d "cd
vnoremap c "cc
vnoremap <leader>s :s/

" 折行内移动
noremap <silent><expr> j (v:count == 0 ? 'g<down>' : 'j')
noremap <silent><expr> k (v:count == 0 ? 'g<up>' : 'k')

call plug#begin('~/.config/nvim/plugged')
"Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && yarn install'  }
"Plug 'vim-airline/vim-airline'
"Plug 'vim-airline/vim-airline-themes'
Plug 'itchyny/lightline.vim'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'justinmk/vim-sneak'
Plug 'mhinz/vim-startify'
Plug 'morhetz/gruvbox'
Plug 'kien/ctrlp.vim'
Plug 'preservim/nerdcommenter'
Plug 'Yggdroot/indentLine'
Plug 'luochen1990/rainbow'
Plug 'Shougo/defx.nvim', { 'do': ':UpdateRemotePlugins' }
Plug 'gcmt/wildfire.vim'
Plug 'sbdchd/neoformat'
Plug 'majutsushi/tagbar'
Plug 'puremourning/vimspector'
Plug 'christoomey/vim-tmux-navigator'
call plug#end()
" ----------------------------------------
" markdown-preview
" 自动打开预览页面
"let g:mkdp_auto_start=1
" ----------------------------------------
"  vimspector
let g:vimspector_enable_mappings = 'VISUAL_STUDIO'
nnoremap <leader>e :VimspectorReset<cr>
" ----------------------------------------
" tagbar
let g:tagbar_width=30
nnoremap <silent> tu :TagbarToggle<CR>
" ----------------------------------------
" neoformat
nnoremap <leader>rf :Neoformat<cr>
" 设置neoformet只做简单的format
" enbale alignment
let g:neoformat_basic_format_align=1

" enbale tab to spaces conversion
let g:neoformat_basic_format_retab = 1

" enable trimming of trailing whitespace
let g:neoformat_basic_format_trim = 1
" ----------------------------------------
" wildfire
" This selects the next closest text object.
map <leader><space> <Plug>(wildfire-fuel)

" This selects the previous closest text object.
vmap <c-p> <Plug>(wildfire-water)
" defx
" ----------------------------------------
nnoremap tt :Defx<cr>
call defx#custom#option('_', {
            \ 'winwidth': 30,
            \ 'split': 'vertical',
            \ 'direction': 'topleft',
            \ 'show_ignored_files': 0,
            \ 'buffer_name': '',
            \ 'toggle': 1,
            \ 'resume': 1
            \ })

autocmd FileType defx call s:defx_my_settings()
function! s:defx_my_settings() abort
    " Define mappings
    nnoremap <silent><buffer><expr> n
                \ defx#do_action('drop')
    nnoremap <silent><buffer><expr> c
                \ defx#do_action('copy')
    nnoremap <silent><buffer><expr> m
                \ defx#do_action('move')
    nnoremap <silent><buffer><expr> p
                \ defx#do_action('paste')
    nnoremap <silent><buffer><expr> l
                \ defx#do_action('open', 'vsplit')
    nnoremap <silent><buffer><expr> P
                \ defx#do_action('preview')
    nnoremap <silent><buffer><expr> o
                \ defx#do_action('open_tree', 'toggle')
    nnoremap <silent><buffer><expr> B
                \ defx#do_action('new_directory')
    nnoremap <silent><buffer><expr> N
                \ defx#do_action('new_file')
    nnoremap <silent><buffer><expr> M
                \ defx#do_action('new_multiple_files')
    nnoremap <silent><buffer><expr> C
                \ defx#do_action('toggle_columns',
                \                'mark:indent:icon:filename:type:size:time')
    nnoremap <silent><buffer><expr> S
                \ defx#do_action('toggle_sort', 'time')
    nnoremap <silent><buffer><expr> d
                \ defx#do_action('remove')
    nnoremap <silent><buffer><expr> r
                \ defx#do_action('rename')
    nnoremap <silent><buffer><expr> !
                \ defx#do_action('execute_command')
    nnoremap <silent><buffer><expr> x
                \ defx#do_action('execute_system')
    nnoremap <silent><buffer><expr> yy
                \ defx#do_action('yank_path')
    nnoremap <silent><buffer><expr> .
                \ defx#do_action('toggle_ignored_files')
    nnoremap <silent><buffer><expr> ;
                \ defx#do_action('repeat')
    nnoremap <silent><buffer><expr> h
                \ defx#do_action('cd', ['..'])
    nnoremap <silent><buffer><expr> ~
                \ defx#do_action('cd')
    nnoremap <silent><buffer><expr> q
                \ defx#do_action('quit')
    nnoremap <silent><buffer><expr> <Space>
                \ defx#do_action('toggle_select') . 'j'
    nnoremap <silent><buffer><expr> *
                \ defx#do_action('toggle_select_all')
    nnoremap <silent><buffer><expr> j
                \ line('.') == line('$') ? 'gg' : 'j'
    nnoremap <silent><buffer><expr> k
                \ line('.') == 1 ? 'G' : 'k'
    "nnoremap <silent><buffer><expr> <C-l>
                "\ defx#do_action('redraw')
    nnoremap <silent><buffer><expr> <C-g>
                \ defx#do_action('print')
    nnoremap <silent><buffer><expr> b
                \ defx#do_action('change_vim_cwd')
endfunction
" ----------------------------------------
" rainbow
let g:rainbow_active=1
" ----------------------------------------
" indentLine
let g:indent_guides_guide_size            = 1  " 指定对齐线的尺寸
let g:indent_guides_start_level           = 2  " 从第二层开始可视化显示缩进

" ----------------------------------------
" m-sneak vim-sneak
let g:sneak#label = 1
let g:sneak#use_ic_scs=1
map f <Plug>Sneak_s
map F <Plug>Sneak_S
map s <Plug>Sneak_f
map S <Plug>Sneak_F
" ----------------------------------------
" colorscheme
colorscheme gruvbox
set background=dark
highlight Normal guibg=NONE ctermbg=None
"let g:SnazzyTransparent = 1

" ----------------------------------------
" NERDTree
"nnoremap te :NERDTreeToggle<CR>
"nnoremap tf :NERDTreeFind<cr>
"let NERDTreeOpenVSplit = ""
"autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
"let g:NERDTreeShowLineNumbers=1 " 是否显示行号
" ----------------------------------------
" coc.nvim
let g:coc_global_extensions=['coc-json', 'coc-python', 'coc-highlight', 'coc-cmake', 'coc-git', 'coc-pairs', 'coc-clangd', 'coc-tsserver']
set hidden
set updatetime=100
set shortmess+=c

" Use tab for trigger completion with characters ahead and navigate.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
            \ pumvisible() ? "\<C-n>" :
            \ <SID>check_back_space() ? "\<TAB>" :
            \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

function! s:check_back_space() abort
    let col = col('.') - 1
    return !col || getline('.')[col - 1]  =~# '\s'
endfunction
" GoTo code navigation.
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
nmap <silent> gb <C-o>
" Use <leader>d to show documentation in preview window.
nnoremap <silent> <leader>d :call <SID>show_documentation()<CR>

function! s:show_documentation()
    if (index(['vim','help'], &filetype) >= 0)
        execute 'h '.expand('<cword>')
    elseif (coc#rpc#ready())
        call CocActionAsync('doHover')
    else
        execute '!' . &keywordprg . " " . expand('<cword>')
    endif
endfunction
" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming.
nmap <leader>rn <Plug>(coc-rename)

" Run Code
map <leader>rr :call Run()<CR>
func! Run()
    exec "w"
    if &filetype == 'sh'
        :!bash %
    elseif &filetype == 'python'
        exec "!python %"
    endif
endfunc
map <leader>rc :call Compile()<cr>
func! Compile()
    exec "w"
    if &filetype == 'c' || &filetype == 'cpp'
        if filereadable('CMakeLists.txt')
            if isdirectory('build')
                exec "cd build"
                exec "!cmake .."
                exec "!make"
                exec "cd .."
            else
                exec "!mkdir build"
                exec "cd build"
                exec "!cmake .."
                exec "!make"
                exec "cd .."
            endif
            "echo "Compile successfully!"
        else
            exec "!touch CMakeLists.txt"
            echo "There is no CMakeLists.txt, we created a new one for you."
        endif
    elseif &filetype == 'typescript'
        exec "!tsc %"
        "echo "Compile successfully!"
    endif
endfunc
